package com.example.system.information;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;

import com.example.system.information.log.LogEntry;
import com.example.system.information.log.LogUtils;
import com.example.system.information.log.MQClientUtil;
import com.example.system.information.mq.MQUtils;
import com.example.system.information.service.MQPushService;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import okhttp3.OkHttpClient;
import okhttp3.Request;

/**
 * 终端管理-获取设备系统信息
 */
public class MainActivity extends AppCompatActivity {
    private static final String TAG = MainActivity.class.getSimpleName();
    public static Activity mActivity;
    private Intent mqServiceIntent;

    private long testStartTime;
    private long testEndTime;
    private LogUtils logUtils;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mActivity = this;

        initView();
    }

    private void initView() {

        //初始化-获取日志数据
        findViewById(R.id.txtx).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MQClientUtil.getInstance(mActivity);
            }
        });

        // 初始化-设备信息服务意图
        mqServiceIntent = new Intent(this, MQPushService.class);
        startService(mqServiceIntent);

        //测试数据
        triggerErrorLogs();
    }

    /**
     * 触发错误日志
     */
    private void triggerErrorLogs() {
        testStartTime = System.currentTimeMillis();
        appendLog("开始触发错误日志...");

        // 1. 空指针异常（捕获但会打印错误日志）
        try {
            String nullStr = null;
            nullStr.length();
        } catch (NullPointerException e) {
            Log.e("TestError", "空指针异常测试", e);
        }

        // 2. 网络错误（访问不存在的地址）
        OkGo.<String>get("https://invalid.url.test")
                .tag(this)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {

                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        Log.e("TestError", "网络错误测试", response.getException());
                    }
                });

        // 3. 数组越界异常
        try {
            int[] arr = new int[1];
            int value = arr[2];
        } catch (ArrayIndexOutOfBoundsException e) {
            Log.e("TestError", "数组越界异常测试", e);
        }

        testEndTime = System.currentTimeMillis();
        appendLog("错误日志触发完成");
    }

    /**
     * 在UI上追加日志显示
     */
    private void appendLog(String text) {
        runOnUiThread(() -> {
            Log.d(TAG, text + "\n");
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mqServiceIntent != null) {
            // 页面销毁时停止服务（可选，根据需求决定）
            stopService(mqServiceIntent);
        }
        //关闭线程池
        MQUtils.shutdown();
    }
}
